(function () {
	var datepicker = {};

	datepicker.getMonthData = function(year,month){
		var res = [];
		if(!year || !month){
			var today = new Date();
			year = today.getFullYear();
			month = today.getMonth()+1;
		}

		// 当月的第一天
		var firstDay = new Date(year,month-1,1);
		var firstDayWeekDay = firstDay.getDay();//周几
		if(firstDayWeekDay===0) firstDayWeekDay = 7;//周日

		// 当月的最后一天
		var lastDay = new Date(year,month,0);//根据 日期数值的越界进位、退位，当月的第0天就是上个月的最后一天
		var lastDate = lastDay.getDate(); 

		// 上个月的最后一天
		var lastDayOfLastMonth = new Date(year,month-1,0);//根据 日期数值的越界进位、退位，当月的第0天就是上个月的最后一天
		var lastDateOfLastMonth = lastDayOfLastMonth.getDate(); //上个月的日期
		//每个月的第一行要显示几个上个月的日期
		var preMonthDayCount = firstDayWeekDay-1;
 
 		// 获取6周的数据
		for (var i=0; i<7*6; i++){
			var date = i-preMonthDayCount;
			var showDate = date;
			var thisMonth = month;
			//如果是上个月的数据
			if(date<=0){
				thisMonth = thisMonth-1;
				showDate = lastDateOfLastMonth+date;
			}else if(date>lastDate){//大于本月最后一天，则是下个月的数据
				thisMonth = thisMonth+1;
				showDate = showDate-lastDate;
			}

			if (thisMonth==0) thisMonth = 12;
			if (thisMonth==13) thisMonth = 1;


			res.push({
				month:thisMonth,
				date:date,
				showDate:showDate
			});	
		}

		return  res;

	}

	datepicker.render = function($dom){
		var html = '';
		var data = datepicker.getMonthData(2017,5); 
		html += '<table>';
		html +='<th>日</th>';
		html +='<th>一</th>';
		html +='<th>二</th>';
		html +='<th>三</th>';
		html +='<th>四</th>';
		html +='<th>五</th>';
		html +='<th>六</th>';
		for(var i=0;i<data.length;i++){
			if (i%7==0) {html += '<tr>';}

				html += '<td>'+data[i].showDate+'</td>'; 

			if (i%7==6) {html += '</tr>';}
		}

		document.querySelector($dom).innerHTML = html;
	}


	window.datepicker = datepicker;//调用window对象，将该对象暴露出去
})()